﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using System.Text;
using Newtonsoft.Json;

public partial class Rep_Editxncstime : System.Web.UI.Page
{
    BLLParamater bll = new BLLParamater();
    DataTable dt = new DataTable();
    DataTable pdt = new DataTable();
    string result;
    protected void Page_Load(object sender, EventArgs e)
    {
        string param = Request["param"];
        if (param != null)
        {
            if (param == "query")
            {
                string data = Request["time"];
                string bc = Request["bc"];
                getResult(data, bc);
            }
            else if (param == "search")
            {
                string data = Request["time"];
                string[] name = HttpUtility.UrlDecode(Request["name"]).Split(';');
                string[] unit = Request["unit"].Split(';');
                string[] value = Request["value"].Split(';');
                string[] num = Request["num"].Split(';');
                string[] key = Request["key"].Split(';');
                string bc = Request["bc"];
                Edit(data, num, name, unit, value, key, bc);
            }
        }

    }

    public void Edit(string data, string[] num, string[] name, string[] unit, string[] value, string[] key, string bc)
    {
        string paramaters = "";
        dt = bll.getOraer(data);
        pdt = bll.getParamaters(data, dt.Rows[Convert.ToInt32(bc)][0].ToString());
        int k = pdt.Rows.Count;
        for (int i = 0; i < name.Length - 1; i++)
        {
            if (pdt.Rows.Count == 0)
                paramaters += "insert into T_PARAMATER_ATTRIBUTE(PARAMATERNUM,VALUE,GROUP,TDATA) values(" + Convert.ToInt32(num[i]) + "," + Convert.ToDouble(value[i]) + ",'" + dt.Rows[Convert.ToInt32(bc)][0] + "','" + Convert.ToDateTime(data).ToString("yyyy-MM-dd HH:mm:ss") + ".000000" + "');";
            else
                paramaters += "update T_PARAMATER_ATTRIBUTE set VALUE=" + Convert.ToDouble(value[i]) + " where ID_KEY=" + key[i] + ";";
        }

        if (bll.Inserts(paramaters))
            result = "参数修改成功!";
        else
            result = "参数修改失败!";
        object obj = new { tableInfo = result };
        result = JavaScriptConvert.SerializeObject(obj);
        Response.Write(result);
        Response.End();
    }

    public void getResult(string data, string bc)
    {
        dt = bll.getOraer(data);
        pdt = bll.getParamaters(data, dt.Rows[Convert.ToInt32(bc)][0].ToString());
        if (pdt != null && pdt.Rows.Count > 0)
        {
            dt = pdt;
        }
        else
        {
            dt = bll.getParamater();
        }

        StringBuilder sp = new StringBuilder();

        sp.Append("<table border='1' bordercolorlight='999999' bordercolordark='#FFFFFF' cellspacing='0' cellpadding='1' width='85%'>");
        sp.Append("<tr bgcolor='#CFE6FC'><td>参数名</td><td>数值</td><td>单位</td></tr>");
        if (dt != null)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sp.Append("<tr>");
                if (dt.Columns.Count > 3)
                {
                    sp.AppendFormat("<td><span id='name" + i + "'>{0}</span>&nbsp;", dt.Rows[i]["NAME"]);
                    sp.AppendFormat("<span id='key" + i + "' style='display:none;'>{0}</span>&nbsp;", dt.Rows[i]["ID_KEY"]);
                    sp.AppendFormat("<span id='num" + i + "' style='display:none;'>{0}</span>&nbsp;</td>", dt.Rows[i]["NUM"]);
                    sp.AppendFormat("<td><input type='text' id='value" + i + "' style='text-align: left; width: 231px;' value='{0}' />&nbsp;</td>", dt.Rows[i]["VALUE"]);
                    sp.AppendFormat("<td><span id='unit" + i + "'>{0}</span>&nbsp;</td>", dt.Rows[i]["UNIT"]);
                }
                else
                {
                    sp.AppendFormat("<td><span id='name" + i + "'>{0}</span>&nbsp;", dt.Rows[i]["NAME"]);
                    sp.AppendFormat("<span id='key" + i + "' style='display:none;'>{0}</span>&nbsp;", (i + 1));
                    sp.AppendFormat("<span id='num" + i + "' style='display:none;'>{0}</span>&nbsp;</td>", dt.Rows[i]["NUM"]);
                    sp.Append("<td><input type='text' id='value" + i + "' style='text-align: left; width: 231px;' value='0.00' />&nbsp;</td>");
                    sp.AppendFormat("<td><span id='unit" + i + "'>{0}</span>&nbsp;</td>", dt.Rows[i]["UNIT"]);

                }
                sp.Append("</tr>");
            }
        }
        else
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sp.Append("<td><input type='text' id='name" + i + "' style='text-align: left; width: 231px;' value='' />&nbsp;</td>");
                sp.AppendFormat("<span id='key" + i + "' style='display:none;'>{0}</span>&nbsp;", (i + 1));
                sp.Append("<span id='num" + i + "' style='display:none;'>" + (i + 1) + "</span></td>");
                sp.Append("<td><input type='text' id='value" + i + "' style='text-align: left; width: 231px;' value='' />&nbsp;</td>");
                sp.Append("<td><input type='text' id='unit" + i + "' style='text-align: left; width: 231px;' value='' />&nbsp;</td>");
            }
        }

        sp.Append("</table>");
        string cookies = "1";
        //try
        //{
        //    cookies = Server.UrlDecode(Request.Cookies["USERGROUP"].Value);// Session["USERGROUP"];
        //}
        //catch (Exception ex)
        //{
        //}
        object obj = new { tableInfo = sp.ToString(), count = dt.Rows.Count, judge = cookies };
        result = JavaScriptConvert.SerializeObject(obj);
        Response.Write(result);
        Response.End();
    }

    /// <summary>   
    /// Json特符字符过滤 20110902
    /// </summary>  
    /// <param name="sourceStr">要过滤的源字符串</param>  
    /// <returns>返回过滤的字符串</returns>  
    public string JsonCharFilter(string sourceStr)
    {
        sourceStr = sourceStr.Replace("\\", "\\\\");
        sourceStr = sourceStr.Replace("\b", "\\\b");
        sourceStr = sourceStr.Replace("\t", "\\\t");
        sourceStr = sourceStr.Replace("\n", "\\\n");
        sourceStr = sourceStr.Replace("\n", "\\\n");
        sourceStr = sourceStr.Replace("\f", "\\\f");
        sourceStr = sourceStr.Replace("\r", "\\\r");
        sourceStr = sourceStr.Replace("'", "\\'");
        return sourceStr.Replace("\"", "\\\"");
    }
}
